package com.chuangxin.lucene;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by Dawnwords on 2015/2/4.
 */
public class Test {
    public static void main(String[] args) throws Exception {
        File inputDir = new File("E:" + File.separator + "bmcx" + File.separator + "swgwsm");
        File indexDir = new File("E:" + File.separator + "bmcx" + File.separator + "swgwsm-index");

//        indexing(inputDir, indexDir);
        String[] keywords = {"蝙蝠", "开心", "早上", "早晨"};
        for (String keyword : keywords) {
            System.out.println("Searching " + keyword);
            query(indexDir, keyword);
            System.out.println("-------------------------");
        }
    }

    private static void indexing(File inputDir, File indexDir) throws IOException {
        if (!indexDir.exists()) {
            indexDir.mkdirs();
        }

        Directory dir = FSDirectory.open(indexDir);
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(Version.LATEST, analyzer);
        IndexWriter writer = new IndexWriter(dir, iwc);

        for (File file : inputDir.listFiles()) {
            Document document = new Document();
            document.add(new StringField("path", file.getPath(), Field.Store.YES));
            document.add(new StringField("title", file.getName(), Field.Store.YES));
            FileReader in = new FileReader(file);
            document.add(new TextField("content", new BufferedReader(in)));
            writer.addDocument(document);
            in.close();
        }

        writer.close();
        System.out.println("Indexing Complete");
    }

    private static void query(File index, String queryString) throws IOException, ParseException {
        IndexReader reader = DirectoryReader.open(FSDirectory.open(index));
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new StandardAnalyzer();
        QueryParser parser = new QueryParser("content", analyzer);
        Query query = parser.parse(queryString);

        ScoreDoc[] hits = searcher.search(query, 100).scoreDocs;
        int i = 1;
        for (ScoreDoc hit : hits) {
            System.out.printf("%d.\tscore=%f :%s\n", i++, hit.score, searcher.doc(hit.doc).get("title"));
        }
        reader.close();
    }
}
